/**
 *  Copyright 2002-2009 the original author or authors.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package foo.bar.wiki.domain;

import javax.persistence.*;
import java.util.*;

/**
 * Represents a wiki space.
 * 
 * @author tmjee
 * @version $Date$ $Id$
 */
@Entity
@Table(name="TBL_SPACE")
@EntityListeners({EntityAuditListener.class})
@NamedQueries({
    @NamedQuery(
        name="Space_findAllSpaces",
        query="SELECT space FROM Space AS space "),
    @NamedQuery(
        name="Space_countAllSpaces",
        query="SELECT count(space) FROM Space AS space"),
    @NamedQuery(
        name="Space_findSpaceById",
        query="SELECT space FROM Space AS space WHERE space.id = :spaceId"),
    @NamedQuery(
        name="Space_findSpaceByName",
        query="SELECT space FROM Space AS space WHERE space.name = :spaceName"),
    @NamedQuery(
        name="Space_findSpaceMembers",
        query="SELECT mem FROM Space AS space INNER JOIN space.members AS mem WHERE space.id = :spaceId AND mem.id <> space.creator.id"),
    @NamedQuery(
        name="Space_countSpaceMembers",
        query="SELECT COUNT(mem) FROM Space AS space INNER JOIN space.members AS mem WHERE space.id = :spaceId AND mem.id <> space.creator.id"),
    @NamedQuery(
        name="Space_findNonSpaceMembers",
        query="SELECT user FROM User AS user WHERE user.id NOT IN (SELECT DISTINCT space.creator.id FROM Space AS space WHERE space.id = :spaceId) AND user.id NOT IN (:memberIds) "),
    @NamedQuery(
        name="Space_countNonSpaceMembers", 
        query="SELECT count(user) FROM User AS user WHERE user.id NOT IN (SELECT DISTINCT space.creator.id FROM Space AS space WHERE space.id = :spaceId) AND user.id NOT IN (:memberIds) "),
    @NamedQuery(
        name="Space_findSpaceMemberIds",
        query="SELECT DISTINCT mem.id FROM Space as space, IN(space.members) AS mem WHERE space.id = :spaceId"),
    @NamedQuery(
        name="Space_findUserMemberSpace",
        query="SELECT space FROM Space AS space INNER JOIN space.members AS mem WHERE mem.id = :userId "),
    @NamedQuery(
        name="Space_findUserOwnerSpace",
        query="SELECT space FROM Space AS space WHERE space.creator = :user")
})
public class Space implements Identifiable, CreationInfoPersistable, ModificationInfoPersistable, PathAware {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;

    @Basic
    @Column(name="NAME")
    private String name;

    @Basic
    @Column(name="DESCRIPTION")
    private String description;

    @Basic
    @Column(name="THEME_NAME")
    private String themeName = "Default";

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="CREATOR_ID", referencedColumnName = "ID")
    private User creator;

    @ManyToOne(targetEntity = User.class)
    @JoinColumn(name="MODIFIER_ID", referencedColumnName = "ID")
    private User modifier;

    @Basic
    @Column(name="CREATION_DATE")
    private Date creationDate;

    @Basic
    @Column(name="MODIFICATION_DATE")
    private Date modificationDate;

    @OneToOne(targetEntity = Page.class)
    @PrimaryKeyJoinColumn
    private Page defaultPage;
    
    @ManyToMany(targetEntity = User.class)
    @JoinTable(
        name="TBL_SPACE_MEMBER",
        joinColumns = {
            @JoinColumn(name="SPACE_ID", referencedColumnName = "ID")
        },
        inverseJoinColumns = {
            @JoinColumn(name="USER_ID", referencedColumnName = "ID")        
        })
    private List<User> members = new ArrayList<User>();

    @OneToMany(targetEntity = SpaceProperty.class, mappedBy = "space")
    private List<SpaceProperty> properties = new ArrayList<SpaceProperty>();


    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getThemeName() {
        return themeName;
    }

    public void setThemeName(String themeName) {
        this.themeName = themeName;
    }


    public void setCreator(User creator) {
        this.creator = creator;
    }

    public User getCreator() {
        return creator;
    }


    public void setModifier(User modifier) {
        this.modifier = modifier;
    }

    public User getModifier() {
        return modifier;
    }


    public Date getCreationDate() {
        return creationDate;
    }

    public void setCreationDate(Date creationDate) {
        this.creationDate = creationDate;
    }

    public Date getModificationDate() {
        return modificationDate;
    }

    public void setModificationDate(Date modificationDate) {
        this.modificationDate = modificationDate;
    }

    public List<User> getMembers() {
        return members;
    }

    public void setMembers(List<User> members) {
        this.members = members;
    }


    public List<SpaceProperty> getProperties() {
        if (properties == null) {
            setProperties(new ArrayList<SpaceProperty>());
        }
        return properties;
    }

    public void setProperties(List<SpaceProperty> properties) {
        this.properties = properties;
    }

    public Page getDefaultPage() {
        return defaultPage;
    }

    public void setDefaultPage(Page defaultPage) {
        this.defaultPage = defaultPage;
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Space space = (Space) o;

        if (name != null ? !name.equals(space.name) : space.name != null) return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (id != null ? id.hashCode() : 0);
        result = 31 * result + (name != null ? name.hashCode() : 0);
        result = 31 * result + (description != null ? description.hashCode() : 0);
        result = 31 * result + (themeName != null ? themeName.hashCode() : 0);
        result = 31 * result + (creationDate != null ? creationDate.hashCode() : 0);
        result = 31 * result + (modificationDate != null ? modificationDate.hashCode() : 0);
        result = 31 * result + (properties != null ? properties.hashCode() : 0);
        return result;
    }

    public String getFullPath() {
        return "/"+name;
    }
}
